package lpl.com.xyd.test.manage.factory;

import com.alibaba.fastjson.JSONObject;
import lpl.com.xyd.test.manage.common.enums.ProjectStatusEnum;
import lpl.com.xyd.test.manage.common.factory.AbstractDomainFactory;
import lpl.com.xyd.test.manage.common.factory.AutowiredDomainFactory;
import lpl.com.xyd.test.manage.dao.ProjectMapperDAO;
import lpl.com.xyd.test.manage.domain.ProjectDomain;
import lpl.com.xyd.test.manage.dto.ProjectDTO;
import lpl.com.xyd.test.manage.param.ProjectParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class ProjectDomainFactory extends AbstractDomainFactory<ProjectDomain, ProjectParam> {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProjectDomainFactory.class);

    @Autowired
    private AutowiredDomainFactory autowiredDomainFactory;

    @Autowired
    private ProjectMapperDAO projectMapperDAO;

    @Override
    public ProjectDomain create(ProjectParam param) {
        LOGGER.info("创建领域，param:{}", JSONObject.toJSONString(param));
        ProjectDomain domain = autowiredDomainFactory.create(ProjectDomain.class);
        BeanUtils.copyProperties(param,domain);
        domain.setStatus(ProjectStatusEnum.getByCode(param.getStatus()));
        return domain;
    }

    @Override
    public ProjectDomain active(String domainId) {
        ProjectDTO projectDTO = projectMapperDAO.selectByPrimaryKey(domainId);
        ProjectDomain domain = new ProjectDomain();
        BeanUtils.copyProperties(projectDTO,domain);
        domain.setStatus(ProjectStatusEnum.getByCode(projectDTO.getStatus()));
        return domain;
    }

    @Override
    public List<ProjectDomain> activeList(ProjectParam param) {
        LOGGER.info("领域激活，param:{}", JSONObject.toJSONString(param));
        List<ProjectDTO> projectDTOS = projectMapperDAO.selectByParam(param);
        if (null == projectDTOS || projectDTOS.size() <= 0){
            return null;
        }
        List<ProjectDomain> domainList = new ArrayList<>(projectDTOS.size());
        projectDTOS.forEach(v->{
            ProjectDomain domain = new ProjectDomain();
            BeanUtils.copyProperties(v,domain);
            domain.setStatus(ProjectStatusEnum.getByCode(v.getStatus()));
            domainList.add(domain);
        });
        return domainList;
    }

    @Override
    public Boolean store(ProjectDomain domain) {
        LOGGER.info("保存领域，domain:{}", JSONObject.toJSONString(domain));
        ProjectDTO projectDTO = new ProjectDTO();
        BeanUtils.copyProperties(domain,projectDTO);
        projectDTO.setStatus(domain.getStatus().getCode());
        int result = projectMapperDAO.insert(projectDTO);
        return result > 0;
    }

    @Override
    public Boolean reStore(ProjectDomain domain) {
        ProjectDTO projectDTO = new ProjectDTO();
        BeanUtils.copyProperties(domain,projectDTO);
       return projectMapperDAO.updateByPrimaryKey(projectDTO) > 0;
    }

    @Override
    public Boolean reStore(List<ProjectDomain> domains) {
        return null;
    }

    @Override
    public boolean delete(Long domainId) {
        return false;
    }

    @Override
    public boolean delete(List<Long> domainId) {
        return false;
    }
}
